from account.account_service import AccountService, accounts_write_lock
import common.http as http
import spider_config
import json
from common.logger import logger

test_accounts = [
    {"userhash":"ba85cc0e69", "userid":"539159713059104397"},
    {"userhash":"776b3919aa", "userid":"56868197392983333"},
    {"userhash":"1e1739b0f0", "userid":"539169015944581653"},
    {"userhash":"59b7759330", "userid":"539169015944581704"},
    {"userhash":"43ce76d95d", "userid":"539159713059105025"},
]
class SuAccountService(AccountService):
    def active(self):
        pass
        # self.fetch()

    def take(self):
        account = super().take()
        if not account:
            for i in range(3):
                try:
                    self._fetch()
                    account = super().take()
                    break
                except Exception as e:
                    logger.error(f"补充速账号失败: {e}")
        return account
        
    def _fetch(self):
        with accounts_write_lock:
            # params = {"token_type": 6, "limit": 40, "max_used": 1, "task_id": self._task_id}
            # rsp = http.get(spider_config.park_token_host + "/api/parktoken/data", params)
            # data = rsp.json()["data"]
            # self._clear()
            # for item in data:
            #     account = json.loads(item["data"])
            #     self._accounts.append(account)
            
            self._accounts = test_accounts

            logger.success(f"补充速账号完成，剩余账号数量: {len(self._accounts)}")


    # def invalid(self, account) -> bool:
    #     success = super().invalid(account)
    #     if success:
    #         with TokenTable() as db:
    #             db.used(6, account["userid"])
    #             db.commit()
    #     return success